package MyArrayList;

import java.util.Arrays;

public class MyArrayList implements IMyArrayList{
    private int useSize=0;
    private int []array;
private int initcapacity=10;

    public MyArrayList(int initcapacity) {
        array=new int[initcapacity];
    }

    public MyArrayList() {
        array=new int[initcapacity];
    }

    public void grow(){
        array= Arrays.copyOf(this.array,2*array.length);
        initcapacity=2*initcapacity;
    }

    public boolean ifFull(){
        if (useSize==initcapacity){
            return true;
        }
        return false;
    }

    @Override
    public void add(int data) {
if(ifFull()){
    grow();
}
array[useSize]=data;
useSize++;
    }

    @Override
    public void add(int pos, int data) {
        if(pos<0||pos>useSize){
            return ;
        }
        if(ifFull()){
            grow();
        }

            for (int i = useSize-1; i >=pos ; i--) {
                array[i+1]=array[i];
            }
            array[pos]=data;

        useSize++;
    }

    @Override
    public boolean contains(int toFind) {
     if (useSize==0){
         return false;
     }
        for (int i = 0; i <useSize ; i++) {
            if (array[i]==toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i <useSize ; i++) {
            if (array[i]==toFind){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        if(pos<0||pos>useSize){
            return -1;
        }
        return array[pos];
    }

    @Override
    public void set(int pos, int value) {
        if(pos<0||pos>useSize){
            return ;
        }
        array[pos]=value;
    }

    @Override
    public void remove(int toRemove) {
        int tag=0;
        for (int i = 0; i <useSize ; i++) {
            if (array[i]==toRemove){
                for (int j = i; j <useSize -1; j++) {
                    array[j]=array[j+1];
                }
            }
        }
     useSize--;
    }

    @Override
    public int size() {
      return useSize;
    }

    @Override
    public void clear() {
useSize=0;
    }

    @Override
    public void display() {
        for (int i = 0; i <useSize ; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();
    }
}
